package cn.sytton.taffecloud.service.system.entity;

import cn.sytton.taffecloud.common.base.annotation.NewId;
import cn.sytton.taffecloud.common.model.base.FieldEntity;
import cn.sytton.taffecloud.common.db.validation.Field;
import cn.sytton.taffecloud.common.db.validation.NotDuplicate;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import java.sql.Date;

/**
 * 用户entity sys_user
 *
 * @author skyrock
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@NotDuplicate(value = {
        @Field(name = {"cellphone"}, message = "手机号码[ %s ]已存在"),
        @Field(name = {"email"}, message = "邮箱地址[ %s ]已存在"),
        @Field(name = {"loginName"}, message = "账号[ %s ]已存在"),
        @Field(name = {"userCode"}, message = "编号[ %s ]已存在")},
        excludeByField="userId")
public class UserEntity extends FieldEntity {

    @NewId
    @NotNull(message = "用户ID不能为空")
    private String userId;    // 用户ID 

    @NotNull(message = "姓名不能为空")
    private String userName;    // 姓名 

    @NotNull(message = "编号不能为空")
    private String userCode;    // 编号 

    @NotNull(message = "账号不能为空")
    private String loginName;    // 账号 

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @NotNull(message = "密码不能为空")
    private String password;    // 密码

    private Integer userStatus = 1;    // 状态, 0:禁用, 1:启用

    private Integer userType = 0;    // 用户类型, 0:虚拟用户, 1:学生, 2:教职工 

    private String cellphone;    // 手机号码 

    @Email
    private String email;    // 邮箱地址 

    private String photo;    // 照片 

    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date expiryDate;    // 有效期

    private String gender;    // 性别, 男: 1 女: 0

    public String getCellphone() {
        return StringUtils.hasText(cellphone) ? cellphone : null;
    }

    public String getEmail() {
        return StringUtils.hasText(email) ? email : null;
    }

//-----以下是关联属性-----
}
